﻿using System;
using System.Diagnostics;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKArchMethodLibrary.ColumnProfileAnalyze;
using YJKCodeBase.WinForm;

namespace ModelChecking.PerformanceAnalysis.ShapeCoeffiecientOfBuilding
{
	[Transaction(TransactionMode.Manual)]
	internal class CmdShapeCoeffiecientOfBuilding : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			ColumnProfileAnalyzer columnProfileAnalyzer = new ColumnProfileAnalyzer(commandData);
			DocumentResultPacker documentResultPacker = new DocumentResultPacker(commandData.Application.ActiveUIDocument.Document, columnProfileAnalyzer);
			if (!documentResultPacker.Init())
			{
				YJKMessageBox.Show("未找到室外地坪标高");
				return Autodesk.Revit.UI.Result.Failed;
			}
			StandardSegmentMaker standardSegmentMaker = new StandardSegmentMaker(documentResultPacker);
			if (!standardSegmentMaker.StandardSegmentInit())
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			ShapeCoefficientOfBuildingCal shapeCoefficientOfBuildingCal = new ShapeCoefficientOfBuildingCal(standardSegmentMaker);
			if (!shapeCoefficientOfBuildingCal.CalShapeCoefficientOfBuilding())
			{
				YJKMessageBox.Show("计算结果为无限大，请检查模型是否闭合。");
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (CmdShapeCoeffiecientOfBuilding._hWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
				CmdShapeCoeffiecientOfBuilding._hWndRevit = new WindowHandle(mainWindowHandle);
			}
			if (CmdShapeCoeffiecientOfBuilding.m_Form != null && !CmdShapeCoeffiecientOfBuilding.m_Form.IsDisposed)
			{
				CmdShapeCoeffiecientOfBuilding.m_Form.Close();
			}
			CmdShapeCoeffiecientOfBuilding.m_Form = new ShapeCoefficientOfBuildingForm(shapeCoefficientOfBuildingCal);
			CmdShapeCoeffiecientOfBuilding.m_Form.Show(CmdShapeCoeffiecientOfBuilding._hWndRevit);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private static ShapeCoefficientOfBuildingForm m_Form;

		public static WindowHandle _hWndRevit;
	}
}
